/**
 * 
 */
package org.ccay.validator.impl;

import java.util.Collection;
import java.util.ResourceBundle;

import javax.inject.Inject;

import org.ccay.core.log.CcayLoggerFactory;
import org.ccay.core.log.ILogger;
import org.ccay.i18n.service.impl.I18nUtil;
import org.ccay.validator.ValidationMessage;
import org.ccay.validator.ValidatorSystemException;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ValidationAwareSupport;
import com.opensymphony.xwork2.config.Configuration;
import com.opensymphony.xwork2.config.ConfigurationManager;
import com.opensymphony.xwork2.config.providers.XWorkConfigurationProvider;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.util.LocalizedTextUtil;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.ValueStackFactory;
import com.opensymphony.xwork2.validator.ActionValidatorManager;

/**
 * 数据验证的工具方法类，用于扩展其他方式的拦截验证方式
 * 
 * 
 * 
 *
 */
public abstract class DataValidationInterceptor{
	/** 日志类 */
	private final ILogger logger = CcayLoggerFactory.getLogger(DataValidationInterceptor.class);
	/** 验证异常对应的error code */
	protected static final String EX_VALIDATION = "ccay.validator.00010001";
	protected static final String EX_VALIDATION_UNKNOWN = "ccay.validator.00010002";
	
	@Inject
	ResourceBundle defaultResourceBundle;
	
	/** 验证管理器 */
	protected ActionValidatorManager actionValidatorManager;
	public void setActionValidatorManager(ActionValidatorManager actionValidatorManager) {
		this.actionValidatorManager = actionValidatorManager;
	}
	
	/**
	 * 处理验证结果
	 * 如有验证错误，则抛出异常
	 * 
	 * @param validatorContext
	 * @throws ValidatorSystemException
	 */
	protected void processValidateResult(DefaultValidatorContext validatorContext) throws ValidatorSystemException {
		if (validatorContext.hasErrors()) {
			ValidatorSystemException e = new ValidatorSystemException(EX_VALIDATION);
			Collection<ValidationMessage> actionErrors = validatorContext.getActionErrorsWithRowInfo();
			Collection<ValidationMessage> fieldErrors = validatorContext.getFieldErrorsWithRowInfo();
			
			e.setActionErrors(actionErrors);
			e.setFieldErrors(fieldErrors);
			throw e;
		}
	}

	/**
	 * 创建ValueStack对象
	 * 
	 * @return
	 */
	protected ValueStack createValueStack() {
        ConfigurationManager configurationManager = new ConfigurationManager();
        configurationManager.addContainerProvider(new XWorkConfigurationProvider());
        Configuration configuration = configurationManager.getConfiguration();
        Container container = configuration.getContainer();
        
        // Reset the value stack
        ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
        valueStack.getContext().put(ActionContext.CONTAINER, container);
        ActionContext.setContext(new ActionContext(valueStack.getContext()));
    
        // clear out localization
        LocalizedTextUtil.reset();
        container.inject(valueStack);
        return valueStack;
    }
    
	/**
	 * 构建上下文
	 * @return
	 */
	protected DefaultValidatorContext createValidatorContext() {
    	ValidationAwareSupport validationAware = new ValidationAwareSupport();
    	// 构造验证上下文
    	DefaultValidatorContext.Builder builder = new DefaultValidatorContext.Builder();
    	
    	DefaultValidatorContext validatorContext = builder.build(validationAware, 
    			I18nUtil.getNullableResourceBundle(defaultResourceBundle));
    	return validatorContext;
    }
    
}
